const app = getApp(),  siteroot = getApp().siteInfo.siteroot;
var p_animation, bout, animation;
let rewardedVideoAd = null;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    awardList: [],
    colorAwardDefault: "#F5F0FC",
    colorAwardSelect: "#ffe400",
    indexSelect: 0,
    isRunning: !1,
    animationData: "",
    prize: !1,
    lottert: 0,
    close: !1,
    lotteryList: [],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var n = this;
    wx.request({
      url: siteroot + 'wxapp/api/getLotteryList',
      success: function(res) {
        var e = res.data;
        console.log(e);
        n.setData({
          lotteryList: e.data
        })
      }
    });
    if(wx.createRewardedVideoAd) {
      rewardedVideoAd = wx.createRewardedVideoAd({ adUnitId: 'adunit-c5e84b6ddfd930f3' });
      rewardedVideoAd.onLoad(() => {
        console.log('onLoad event emit')
      })
      rewardedVideoAd.onError((err) => {
        console.log('onError event emit', err)
      })
      rewardedVideoAd.onClose((res) => {
        console.log('onClose event emit', res);
        // 用户点击了【关闭广告】按钮
        if (res && res.isEnded) {
          // 正常播放结束，可以下发游戏奖励
          n.setData({
            oppty: 1
          })
        } else {
          // 播放中途退出，不下发游戏奖励
          wx.showToast({
            title: '视频终止，无法参加抽奖',
            icon: 'none',
            duration: 2000
          })
        }
      })
    }
  },
  rewardedAd: function() {
    rewardedVideoAd.show()
    .catch(() => {
      rewardedVideoAd.load()
      .then(() => rewardedVideoAd.show())
      .catch(err => {
        console.log('激励视频 广告显示失败')
      })
    });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var n = this;
    wx.request({
      url: siteroot + 'wxapp/api/getPond',
      data: {openid: wx.getStorageSync('openid')},
      success: function(t) {
        n.setData(t.data.data);
        for (var e = t.data.data.list, a = 18, o = 18, i = 0; i < 8; i++) 0 == i ? o = a = 18 : i < 3 ? (a = a, 
          o = o + 196 + 8) : i < 5 ? (o = o, a = a + 158 + 8) : i < 7 ? (o = o - 196 - 8, 
          a = a) : i < 8 && (o = o, a = a - 158 - 8), e[i].topAward = a, e[i].leftAward = o;
          n.setData({
            awardList: e
          });
      },
      complete: function(t) {
        n.pond_animation();
      }
    })
  },

  startGame: function() {
    var n = this;
    app.myLogin();
    if(!n.data.isRunning) {
      var t = "";
      if(0 == n.data.oppty && (t = '抽奖机会已用完'), n.data.time || (t == '活动未开始或已经结束'), t) wx.showModal({
        title: "很抱歉",
        content: t,
        showCancel: !1,
        success: function(t) {
          t.confirm && n.setData({
            isRunning: !1
          })
        }
      }); else {
        clearInterval(p_animation), animation.translate(0, 0).step(), n.setData({
          animationData: animation.export(),
          isRunning: !0,
          lottert: 0
        });
        var e = n.data.indexSelect, a = 0, s = n.data.awardList, r = setInterval(function() {
          if (e++, e %= 8, a += 30, n.setData({
              indexSelect: e
          }), 0 < n.data.lottert && e + 1 == n.data.lottert) {
              if (clearInterval(r), n.pond_animation(), 5 == s[e].type) var t = 1; else t = 2;
              n.setData({
                  isRunning: !1,
                  name: s[e].name,
                  num: s[e].id,
                  prize: t
              });
          }
        }, 200 + a);
        wx.request({
          url: siteroot + 'wxapp/api/getPondLottery',
          data: {openid: wx.getStorageSync('openid')},
          success: function(res) {
            var o = res.data;
            if( 1 == o.code) return clearInterval(r), wx.showModal({
              title: "很抱歉",
              content: o.msg ? o.msg : '网络错误',
              showCancel: !1,
              success: function(t) {
                t.confirm && n.setData({
                  isRunning: !1
                });
              }
            }), void n.pond_animation();
            var i = o.data.id;
            s.forEach(function(t, e, a) {
              t.id == i && setTimeout(function() {
                n.setData({
                  lottert: e + 1,
                  oppty: o.data.oppty
                });
              }, 2e3);
            });
          }
        })
      }
    }
  },
  pondClose: function() {
    this.setData({
        prize: !1
    });
  },
  pond_animation: function() {
    var t = this;
    animation = wx.createAnimation({
        duration: 500,
        timingFunction: "step-start",
        delay: 0,
        transformOrigin: "50% 50%"
    });
    var e = !0;
    p_animation = setInterval(function() {
        e ? (animation.translate(0, 0).step(), e = !1) : (animation.translate(0, -3).step(), 
        e = !0), t.setData({
            animationData: animation.export()
        });
    }, 900);
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})